/**
 * 
 */
package com.ez.supervise.service.impl;

import java.util.List;

import org.apache.commons.lang.math.NumberUtils;

import com.ez.supervise.core.Page;
import com.ez.supervise.dao.GoalDetailDao;
import com.ez.supervise.dao.OrgCheckDao;
import com.ez.supervise.model.biz.GoalDetail;
import com.ez.supervise.model.biz.OrgCheck;
import com.ez.supervise.service.OrgCheckService;

/**
 * @author searph.wang
 * @email searph.wang@gmail.com
 * @Created at 2009-7-22
 * 
 */
public class OrgCheckServiceImpl implements OrgCheckService {
    private OrgCheckDao orgCheckDao;
    private GoalDetailDao goalDetailDao;

    // public int update(OrgCheck org) {
    // return sqlMapClientTemplate.update("sys.updateOrgCheck", org);
    // }

    public void setOrgCheckDao(OrgCheckDao orgCheckDao) {
	this.orgCheckDao = orgCheckDao;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#delete(java.lang.Long)
     */
    public void delete(Long id) {
	orgCheckDao.delete(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#insert(java.lang.Object)
     */
    public void insert(Object entity) {
	orgCheckDao.insert(entity);

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#update(java.lang.Object)
     */
    public void update(Object entity) {
	orgCheckDao.update(entity);
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.base.BaseService#get(java.lang.Long)
     */
    public Object get(Long id) {
	return (OrgCheck) orgCheckDao.get(id);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.ez.supervise.service.OrgCheckService#findByPage(com.ez.supervise.core.
     * Page)
     */
    public Page findByPage(Page page) {
	page.setTotalProperty(orgCheckDao.findOrgCheckByCount(page));
	page.setRoot(orgCheckDao.findOrgCheckByPage(page));
	return page;

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ez.supervise.service.OrgCheckService#findAll()
     */
    public List findAll() {
	return orgCheckDao.findAll();
    }

    /* (non-Javadoc)
     * @see com.ez.supervise.service.OrgCheckService#findGoalByType(java.lang.String)
     */
    public List findGoalByType(String type) {
	return orgCheckDao.findGoalByType(type);
    }

    public void findOrgCheckByPage(Page page) {
	page.setRoot(orgCheckDao.findOrgCheckByPage(page));
	page.setTotalProperty(orgCheckDao.findOrgCheckByCount(page));	
    }
    
    public void findOrgCheckSettingByPage(Page page) {
	page.setRoot(orgCheckDao.findOrgCheckSettingByPage(page));
	page.setTotalProperty(orgCheckDao.findOrgCheckSettingByCount(page));	
    }

    public void save(Long orgId, String goalIds, String goalType) {
	String[] goalId = goalIds.split(",");
	OrgCheck orgCheck = null;
	GoalDetail goalDetail = null;
	for (String gId : goalId) {
	    if (NumberUtils.isNumber(gId)) {
		orgCheck = new OrgCheck(orgId, Long.valueOf(gId), goalType);
		orgCheck.setCheckStatus(OrgCheck.CHECK_STATUS_NEW);
		int count = orgCheckDao.countExisOrgCheck(orgCheck);
		if (count == 0) {
		    goalDetail = (GoalDetail) goalDetailDao.get(orgCheck.getDetailId());
		    orgCheck.setYear(goalDetail.getYear()); 
		    orgCheck.setCheckOrgId(goalDetail.getCheckOrgId()); 
		    orgCheckDao.insert(orgCheck);
		}
	    }
	}
    }
    
    public void deleteOrgCheckSetting(Long id) {
	orgCheckDao.deleteOrgCheckSetting(id);
    }
    
    public void setGoalDetailDao(GoalDetailDao goalDetailDao) {
        this.goalDetailDao = goalDetailDao;
    }

    public void findOrgCheckSelfByPage(Page page) {
	page.setRoot(orgCheckDao.findOrgCheckSelfByPage(page));
	page.setTotalProperty(orgCheckDao.findOrgCheckSelfByCount(page));
    }

    public void findOrgCheckAuditByPage(Page page) {
	page.setRoot(orgCheckDao.findOrgCheckAuditByPage(page));
	page.setTotalProperty(orgCheckDao.findOrgCheckAuditByCount(page));
    }

 
    
}
